package org.kl.bpm.service.conf;

import java.util.List;
import java.util.Map;

import org.kl.bf.utils.CRUDUtils;
import org.kl.bpm.entity.conf.BpmMailTemplate;
import org.kl.bpm.repository.conf.BpmMailTemplateDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

/**
 * 
 * @Author hqm
 * @CreateTime 2014年5月25日 下午8:40:29
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class BpmMailTemplateService {

	private BpmMailTemplateDao bpmMailTemplateDao;

	public BpmMailTemplate getBpmMailTemplate(String id) {
		return bpmMailTemplateDao.findOne(id);
	}

	public void saveBpmMailTemplate(BpmMailTemplate entity) {
		bpmMailTemplateDao.save(entity);
	}

	public void deleteBpmMailTemplate(String id) {
		bpmMailTemplateDao.delete(id);
	}

	public List<BpmMailTemplate> getAllBpmMailTemplate() {
		return bpmMailTemplateDao.findAll();
	}

	public Page<BpmMailTemplate> getUserBpmMailTemplate(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<BpmMailTemplate> spec = buildSpecification(searchParams);

		return bpmMailTemplateDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<BpmMailTemplate> buildSpecification(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<BpmMailTemplate> spec = DynamicSpecifications.bySearchFilter(filters.values(), BpmMailTemplate.class);
		return spec;
	}

	@Autowired
	public void setBpmMailTemplateDao(BpmMailTemplateDao bpmMailTemplateDao) {
		this.bpmMailTemplateDao = bpmMailTemplateDao;
	}

}
